#coding:utf-8

import os
import json
from xmlrpc.client import ServerProxy
from tty_menu import tty_menu

def GetFileList(dir, fileList):
    newDir = dir
    if os.path.isfile(dir):
        file_name = dir.split("/")[-1]
        if file_name[-3:] == ".py" and file_name != "__init__.py":
            fileList.append(file_name[:-3])
        elif file_name[-5:] == ".json" and file_name != "__init__.py":
            fileList.append(file_name[:-5])

    elif os.path.isdir(dir):
        for s in os.listdir(dir):
            newDir=os.path.join(dir,s)
            GetFileList(newDir, fileList)

    return fileList


if __name__ == "__main__":
    config_file_list = GetFileList('./JsonConfigs/', [])
    pos = tty_menu(config_file_list, "please select fuzz config?")
    print("Your select config is <%s>" % (config_file_list[pos]))

    if config_file_list[pos] == "default":
        config_name = "default"
    else:
        config_name = config_file_list[pos]
        is_celery = True

    with open("./JsonConfigs/%s.json" % config_name, "r") as load_f:
        load_dict = json.load(load_f)

    load_dict =dict(load_dict)

    print("fuzz start...\r\n")
    proxy = ServerProxy("http://127.0.0.1:8000/",allow_none=True)
    proxy.importFuzz("ftp",load_dict)
    proxy.boofuzzRun("rumpusd","")
    